﻿using System.Collections.Generic;
using System.Linq;
using iSteam.Sdk.Data;

namespace iSteam.Sdk.SmartObjects
{
    public class iGroup : iBase
    {
        private GroupInfo _group;
        private AdditionalGroupInfo _additionalGroupInfo;
        
        private List<iUser> _members;

        private LoadType _loadType;

        public iGroup(SteamToken token, string steamId, LoadType loadType = LoadType.OnRequest) : base (token, steamId)
        {
            Ini(loadType);
        }

        private void Ini(LoadType loadType)
        {
            _loadType = loadType;
            switch (loadType)
            {
                case LoadType.All:
                    _group = Core.GetGroupInfo(SteamId);
                    _additionalGroupInfo = Core.GetAdditionalGroupInfo(SteamId);
                    break;
                case LoadType.DataOnly:
                    _group = Core.GetGroupInfo(SteamId);
                    break;
            }
        }
        
        public void Refresh(RefreshType refreshType)
        {
            switch (refreshType)
            {
                case RefreshType.All:
                    _group = _loadType == LoadType.OnRequest ? null : Core.GetGroupInfo(SteamId);
                    _additionalGroupInfo = _loadType == LoadType.OnRequest ? null : Core.GetAdditionalGroupInfo(SteamId);
                    break;
                case RefreshType.UserList:
                    _additionalGroupInfo = _loadType == LoadType.OnRequest ? null : Core.GetAdditionalGroupInfo(SteamId);
                    break;
                case RefreshType.Data:
                    _group = _loadType == LoadType.OnRequest ? null : Core.GetGroupInfo(SteamId);
                    break;
            }

        }

        #region Properties

        public GroupInfo Data
        {
            get
            {
                if (_group == null)
                    _group = Core.GetGroupInfo(SteamId);
                return _group;
            }
        }
        public List<iUser> Members
        {
            get
            {
                if (_members == null)
                {
                    _additionalGroupInfo = Core.GetAdditionalGroupInfo(SteamId);
                    if (_additionalGroupInfo == null) return Members;
                    _members = _additionalGroupInfo.Members.Select(x => new iUser(Token, x.ToString())).ToList();
                }
                return _members;
            }
        }

        public LoadType LoadType
        {
            get { return _loadType; }
            set { _loadType = value; }
        }
        #endregion
    }
}